function which_page() {
    return $('#verify').val();
}

function msg_to_addon() {
    var cur_page = which_page();

    if(cur_page == "custom_bbs") {
        var is_main_ad=$("#main_ad").attr("checked");
        var is_bbs_rules=$("#bbs_rules").attr("checked");
        var is_lvl2=$("#level_2").attr("checked");
        var ans = 0;

        if(is_main_ad == "checked")
            ans += 1;
        if(is_bbs_rules == "checked")
            ans+=2;
        if(is_lvl2 == "checked")
            ans+=4;
    } else if(cur_page == "all_block") {
        var ans = "";
        ans += $("#blk_persons").val();
        ans += "&";
        ans += $("#blk_signs").val();
        ans += "&";
        ans += $("#blk_heads").val();
        ans += "&";
        ans += $("#blk_pics").val();
        ans += "&";
        ans += $("#blk_articles").val();
    } else if(cur_page == "logins") {
        var ans = "";
        var keep_login=$("#keep_login").attr("checked");
        var login_nowait=$("#login_nowait").attr("checked");

        ans += $("input[name='inner_net']:checked").val();
        ans += "&";
        ans += $("input[name='outter_net']:checked").val();
        ans += "&";
        ans += $("input[name='auto_sel']:checked").val();
        ans += "&";
        if(keep_login == "checked")
            ans += "1";
        else
            ans += "0";
        ans += "&";
        if(login_nowait == "checked")
            ans += "1";
        else
            ans += "0";
    }
    return cur_page+":"+ans;
}

window.addEventListener('click', function(event) {
    var cur_name = event.target.name.toString();
    var cur_id = event.target.id;
    // none:0 main_ad:1 bbs_rules:2 both:3
    // 按下保存按钮之后,应该根据页面隐藏内容继续判断,下一步
    // 该用哪个函数继续处理,以确定回发的消息

    // 每次新进页面,需要请求初始化页面
    if(cur_id == "mainad" || cur_id == "blocks" || cur_id == "logins")
        self.port.emit("send_me_init_msg","");
    if(cur_name == "save_conf") {
        self.port.emit('click', msg_to_addon());
        event.stopPropagation();
        event.preventDefault();
    }
}, false);

self.port.on('init_page', function(message) {
    var cur_page = which_page();

    if(cur_page == "custom_bbs") {
        if(message.rm_main_ad == 1) {
            $("#main_ad").attr("checked","checked");
        }
        if(message.rm_bbs_rules == 1) {
            $("#bbs_rules").attr("checked","checked");
        }
        if(message.hide_level_2 == 1) {
            $("#level_2").attr("checked","checked");
        }
    } else if(cur_page == "all_block") {
        $("#blk_persons").val(message.blk_persons);
        $("#blk_signs").val(message.blk_signs);
        $("#blk_heads").val(message.blk_heads);
        $("#blk_pics").val(message.blk_pics);
        $("#blk_articles").val(message.blk_articles);
    } else if(cur_page == "logins") {
        if(message.inner == "v4")
            $('input[name=inner_net]:nth(0)').attr('checked',true);
        else
            $('input[name=inner_net]:nth(1)').attr('checked',true);

        if(message.outter == "qsh")
            $('input[name=outter_net]:nth(0)').attr('checked',true);
        else
            $('input[name=outter_net]:nth(1)').attr('checked',true);

        if(message.auto_domain == "auto")
            $('input[name=auto_sel]:nth(0)').attr('checked',true);
        else if(message.auto_domain == "outter")
            $('input[name=auto_sel]:nth(1)').attr('checked',true);
        else
            $('input[name=auto_sel]:nth(2)').attr('checked',true);

        if(message.keep_login == 1) {
            $("#keep_login").attr("checked","checked");
        }

        if(message.login_nowait == 1) {
            $("#login_nowait").attr("checked","checked");
        }
    }
});
